package com.yanfan.maintain.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.iot.domain.Device;
import com.yanfan.maintain.domain.ControlFormwork;
import com.yanfan.maintain.domain.ControlRuleRequest;
import com.yanfan.maintain.service.IControlFormworkService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 头注释Controller
 *
 * @author kerwincui
 * @date 2023-03-13
 */
@RestController
@RequestMapping("/iot/controlFormwork")
public class ControlFormworkController extends BaseController {

    @Resource
    private IControlFormworkService controlFormworkService;

    @ApiOperation("查询状态")
    @GetMapping("/getStatus")
    public AjaxResult getStatus() {
        List<Map> result = new ArrayList<>();
        Map map = new HashMap();
        map.put("name", "停用");
        map.put("value", 0);
        Map map1 = new HashMap();
        map1.put("name", "启用");
        map1.put("value", 1);

        result.add(map);
        result.add(map1);
        return AjaxResult.success(result);
    }

    @ApiOperation("查询分类")
    @GetMapping("/getCategory")
    public AjaxResult getCategory() {
        List<Map> result = controlFormworkService.getCategory();
        return AjaxResult.success(result);
    }

    @ApiOperation("根据分类查询参数")
    @GetMapping("/getParam")
    public AjaxResult getParam(String category, Long deviceId) {
        List<Map> result = controlFormworkService.getParam(category, deviceId);
        return AjaxResult.success(result);
    }

    @ApiOperation("查询条件")
    @GetMapping("/getCondition")
    public AjaxResult getCondition() {
        List<Map> result = controlFormworkService.getCondition();
        return AjaxResult.success(result);
    }

    @ApiOperation("反控查询属性")
    @GetMapping("/getAttr")
    public AjaxResult getAttr() {
        List<Map> result = controlFormworkService.getAttr();

        return AjaxResult.success(result);
    }

    @ApiOperation("反控查询类型")
    @GetMapping("/getIotDataType")
    public AjaxResult getIotDataType() {
        List<Map> result = controlFormworkService.getIotDataType();

        return AjaxResult.success(result);
    }

    @ApiOperation("查找设备列表")
    @GetMapping("/getDeviceList")
    public AjaxResult getDeviceList(String text) {
        List<Device> result = controlFormworkService.getDeviceList(text);

        return AjaxResult.success(result);
    }

    /**
     * 新增模板
     */
    @ApiOperation("新增模板")
    //@PreAuthorize("@ss.hasPermi('iot:formwork:add')")
    @Log(title = "头注释", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ControlFormwork controlFormwork) {
        return toAjax(controlFormworkService.insertControlFormwork(controlFormwork));
    }

    @ApiOperation("编辑模板")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ControlFormwork controlFormwork) {
        return toAjax(controlFormworkService.updateControlFormwork(controlFormwork));
    }

    /**
     * 查询模板
     */
    @ApiOperation("查询模板")
    //@PreAuthorize("@ss.hasPermi('iot:formwork:list')")
    @GetMapping("/list")
    public TableDataInfo list(ControlFormwork controlFormwork) {
        startPage();
        List<ControlFormwork> list = controlFormworkService.selectControlFormworkList(controlFormwork);
        return getDataTable(list);
    }

    @ApiOperation("为模板添加设备")
    @PostMapping("/addDevice")
    public AjaxResult addDevice(@RequestBody List<ControlRuleRequest> controlRuleRequest) {
        controlFormworkService.addDevice(controlRuleRequest);
        return AjaxResult.success();
    }

    @ApiOperation("根据模板id查询设备")
    @GetMapping("/getDeviceByFormworkId/{id}")
    public AjaxResult getDeviceByFormworkId(@PathVariable("id") Long formworkId) {
        List<ControlRuleRequest> controlRuleRequest = controlFormworkService.getDeviceByFormworkId(formworkId);
        return AjaxResult.success(controlRuleRequest);
    }

    /**
     * 获取头注释详细信息
     */
    //@PreAuthorize("@ss.hasPermi('iot:formwork:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(controlFormworkService.selectControlFormworkById(id));
    }

    @ApiOperation("删除设备")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ControlFormwork controlFormwork) {
        return toAjax(controlFormworkService.deleteControlFormworkById(controlFormwork.getId()));
    }

}
